// jjwm/searchAddress/searchAddress.js
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowAddress: "",
    StrogeAddress: "",
    list:[],
    searchArr:[]
  },
  /**
   * 自定义事件
   */
  Onsearch: function(e) {
    qqmapsdk.search({
      keyword: e.detail.value,
      success: (res) =>{
        // console.log(res);
        this.setData({
          searchArr:res.data
        })
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {
        console.log(res);
      }
    });
  },
  SetStorage:function(e){
    console.log(e.currentTarget.dataset)
    wx.setStorage({
      key: 'SHDZ-ADDRESS',
      data: e.currentTarget.dataset.item,
    })
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    qqmapsdk = new QQMapWX({
      key: 'LTCBZ-XHKCU-HKHVU-BSKGO-B7ZAK-6TBIE'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.getStorage({
      key: 'NOW_ADDRESS',
      success: (res) => {
        console.log(res.data)
        this.setData({
          StrogeAddress: res.data
        })
      },
    })
    wx.getLocation({
      altitude: true,
      success: (res) => {

        qqmapsdk.reverseGeocoder({ //需要开发者后台配置request合法域名，添加https://apis.map.qq.com
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          get_poi: 1,
          poi_options: 'policy=2;radius=5000;page_size=10;page_index=1',
          success: (res) => {
            // console.log(res.result)
            let add = res.result['address_component'].city + res.result['address_component'].district + res.result['address_component'].street_number
            this.setData({
              nowAddress: add
            })
          }
        })
        qqmapsdk.search({
          keyword: "保定市涿州市",
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: (local) => {
            // console.log(local)
            this.setData({
              list:local.data
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})